"""
#
# Flow stability for dynamic community detection https://arxiv.org/abs/2101.06131v2
#
# Copyright (C) 2021 Alexandre Bovet <alexandre.bovet@maths.ox.ac.uk>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


creates the temporal network (ContTempNetwork) from the preprocessed network data
and saves it as `aps_monthly_lcc_net.pickle`


"""


import sys
import os
PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

import pandas as pd
from TemporalNetwork import ContTempNetwork
import json

# raise Exception 
#%%


event_table = pd.read_csv('../data/aps/monthly_lcc_network_events_table.csv.gz', 
                          index_col=0, parse_dates=['pub_date'])

attributes_file = '../data/aps/monthly_lcc_network_attributes.json'

with open(attributes_file, 'r') as fopen:
    
    attributes_dict = json.load(fopen)
    

node_to_label_dict = {int(k):int(v) for k,v in attributes_dict['node_to_label_dict'].items()}    
    
net = ContTempNetwork(events_table=event_table,
                       relabel_nodes=False,
                       node_to_label_dict=node_to_label_dict)
net.time_slices_bound = attributes_dict['time_slices_bounds']

net.time_slices_bounds_datetimes = [pd.to_datetime(d) for d in attributes_dict['time_slices_bounds_datetimes']]

net._compute_time_grid()

filename = '../paper_data/aps/aps_monthly_lcc_net.pickle'

net.save(filename,
            attributes_list=['node_to_label_dict',
                             'events_table',
                             'times',
                             'time_grid',
                             'num_nodes',
                             'time_slices_bounds',
                             'time_slices_bounds_datetimes'])
